package cn.cjxy;

import cn.cjxy.domain.User;
import cn.cjxy.mapper.UserMapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

/**
 * 测试类
 */
@SpringBootTest(classes = Spring_MPApp.class)//启动类的字节码文件
@RunWith(SpringRunner.class)
public class Demo1 {

    @Autowired
    private UserMapper userMapper;
    //查询所有
    @Test
    public void testSelcet(){
        List<User> users = userMapper.selectList(null);
        for (User user : users) {
            System.out.println(user);
        }
    }

    // 插入一条记录
    @Test
    public void insert(){
        //int insert(T entity);
        User user = new User(11L, "tom", "0", 20, "2020-03-06");
        userMapper.insert(user);
    }
    // 根据 ID 修改
    //int updateById(@Param(Constants.ENTITY) T entity);
    @Test
    public void updateById(){
        //int insert(T entity);
        User user = new User(11L, "maria", "0", 21, "2020-03-06");
        userMapper.updateById(user);
    }
    // 根据 ID 删除
    //int deleteById(Serializable id);
    @Test
    public void deleteById(){
        userMapper.deleteById(11L);
    }
    // 根据 ID 查询
    //T selectById(Serializable id);
    @Test
    public void selectById(){
        userMapper.selectById(10L);
    }
    // 查询（根据ID 批量查询）
   // List<T> selectByIds(@Param(Constants.COLLECTION) Collection<? extends Serializable> idList);
    @Test
    public void selectBatchIds(){
        List<Integer> list = Arrays.asList(1, 2, 3, 4);
        List<User> users = userMapper.selectBatchIds(list);
        for (User user : users) {
            System.out.println(user);
        }
    }

    //根据条件查询数据库
    //List<T> selectByMap(@Param(Constants.COLUMN_MAP) Map<String, Object> columnMap);
    @Test
    public void selectByMap(){
        //封装条件
        HashMap<String, Object> param = new HashMap<>();
        param.put("id",10L);
        param.put("name","Aidan");
        //根据条件查询  SELECT id,name,sex,age,birthday FROM user WHERE (name = ? AND id = ?)
        List<User> users = userMapper.selectByMap(param);
        System.out.println(users);
    }

    //批量删除
    //int deleteBatchIds(@Param(Constants.COLLECTION) Collection<? extends Serializable> idList);
    @Test
    public void deleteBatchIds(){
        List<Integer> list = Arrays.asList(1, 2, 3, 4);
        userMapper.deleteBatchIds(list);
    }

    // 根据 columnMap 条件，删除记录
    //int deleteByMap(@Param(Constants.COLUMN_MAP) Map<String, Object> columnMap);
    //与条件查询一致
}
